<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons')
import resize from './mixins/resize'
import { getToken } from '@/utils/auth'
import { getPeakPeriod } from '@/api/charts'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      listQuery: {
        token: getToken(),
        year: undefined,
        month: undefined,
        day: undefined,
        hour: undefined
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      const date = new Date()
      this.listQuery.year = date.getFullYear()
      this.listQuery.month = date.getMonth() + 1
      this.listQuery.day = date.getDate()
      this.listQuery.hour = date.getHours()
      const colors = ['#5470C6', '#91CC75', '#EE6666']
      getPeakPeriod(this.listQuery).then(response => {
        this.chart.setOption({
          color: colors,
          title: {
            text: '高峰期各站客流压力',
            left: 'center'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            }
          },
          legend: {
            data: ['进站', '出站'],
            left: 'left'
          },
          xAxis: [
            {
              type: 'category',
              axisTick: {
                alignWithLabel: true
              },
              data: response.station
            }
          ],
          yAxis: [
            {
              type: 'value',
              name: '进站',
              min: 0,
              max: 250,
              position: 'right',
              axisLine: {
                show: true,
                lineStyle: {
                  color: colors[0]
                }
              },
              axisLabel: {
                formatter: '{value} 人次'
              }
            },
            {
              type: 'value',
              name: '出站',
              min: 0,
              max: 25,
              position: 'left',
              axisLine: {
                show: true,
                lineStyle: {
                  color: colors[2]
                }
              },
              axisLabel: {
                formatter: '{value} 人次'
              }
            }
          ],
          series: [
            {
              name: '进站',
              type: 'bar',
              data: response.in_pressure
            },
            {
              name: '出站',
              type: 'bar',
              data: response.out_pressure
            }
          ]
        })
      })
    }
  }
}
</script>

